﻿using PagedList;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace SelfMadeMillionaire.Web.Infrastructure.ViewModels.Home
{
    public class MixedPublicationItemModel
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime Date { get; set; }
        public string ExternalUrl { get; set; }
        public string Tooltip { get; set; }
        public bool IsPublication { get; set; }
        public bool IsNews { get; set; }
    }

	public class PublicationNewsListViewModel
	{
		public List<Publication> Publications { get; set; }
        public List<News> News { get; set; }
        public List<MixedPublicationItemModel> MixedList { get; set; }
        public IPagedList<MixedPublicationItemModel> MixedPagedList { get; set; }
        public ProductType SelectedProduct { get; set; }

        public void CreateMixedList()
        {
            MixedList = new List<MixedPublicationItemModel>();
            foreach (Publication item in Publications)
				MixedList.Add(new MixedPublicationItemModel { ID = item.ID, Title = item.Title, Date = item.Date, ExternalUrl = item.ExternalUrl, Tooltip = "", IsPublication = true });
            foreach (News item in News)
                MixedList.Add(new MixedPublicationItemModel { ID = item.ID, Title = item.Title, Date = item.Date, ExternalUrl = item.ExternalUrl, Tooltip = item.Tooltip,  IsNews = true });

            MixedList = MixedList.OrderByDescending(i => i.Date).ToList();
        }

        public void CreateMixedPagedList(int pageIndex, int pageSize, string sort)
        {
            MixedList = new List<MixedPublicationItemModel>();
            foreach (Publication item in Publications)
				MixedList.Add(new MixedPublicationItemModel { ID = item.ID, Title = item.Title, Date = item.Date, ExternalUrl = item.ExternalUrl, Tooltip = "", IsPublication = true });
            foreach (News item in News)
                MixedList.Add(new MixedPublicationItemModel { ID = item.ID, Title = item.Title, Date = item.Date, ExternalUrl = item.ExternalUrl, Tooltip = item.Tooltip, IsNews = true });

            if (!string.IsNullOrEmpty(sort))
            {
                string[] sortArr = sort.Split(',');
                if (sortArr[1] == "0")
                {
                    switch (sortArr[0])
                    {
                        case "0":
                            MixedList = MixedList.OrderBy(i => i.Date).ToList();
                            break;
                        case "1":
                            MixedList = MixedList.OrderBy(i => i.Title.Trim()).ToList();
                            break;
                    }
                }
                else
                {
                    switch (sortArr[0])
                    {
                        case "0":
                            MixedList = MixedList.OrderByDescending(i => i.Date).ToList();
                            break;
                        case "1":
                            MixedList = MixedList.OrderByDescending(i => i.Title.Trim()).ToList();
                            break;
                    }
                }

                MixedPagedList = MixedList.ToPagedList(pageIndex, pageSize);
            }
            else
                MixedPagedList = MixedList.OrderByDescending(i => i.Date).ToPagedList(pageIndex, pageSize);
        }

        
	}
}
